package com.example.bzh.gamecat.ui.society.babysociety;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.base.BaseActivity;
import com.example.bzh.gamecat.bean.SocietyAllPlayBean;
import com.example.bzh.gamecat.bean.SocietyBabyBean;
import com.example.bzh.gamecat.bean.SocietyBabyListBean;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SocietyBabyActivity extends BaseActivity implements SocietyBabyContract.IView{

    @BindView(R.id.tv_title_second)
    TextView tvTitleSecond;
    @BindView(R.id.pull_baby_society)
    PullToRefreshListView pullBabySociety;

    private int current = 0;

    private final int  BABY_BEAN = 0;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case BABY_BEAN:

                    List<SocietyBabyListBean> societyBabyBeans = (List<SocietyBabyListBean>) msg.obj;

                    babyAdapter = new SocietyBabyAdapter(SocietyBabyActivity.this,societyBabyBeans);

                    ListView listView = pullBabySociety.getRefreshableView();

                    View view= LayoutInflater.from(SocietyBabyActivity.this).inflate(R.layout.head_list_all_society,null);
                    //添加listview头视图
                    if (current==0){

                        listView.addHeaderView(view);
                    }

                    listView.setAdapter(babyAdapter);

                    pullBabySociety.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            pullBabySociety.onRefreshComplete();
                        }
                    }, 1000);
                    pullBabySociety.setMode(PullToRefreshBase.Mode.BOTH);
                    initPullTo();
                    pullBabySociety.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                        @Override
                        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                            presenter.initData(initParams());
                            babyAdapter.notifyDataSetChanged();

                        }

                        @Override
                        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                            current++;
                            presenter.initData(initParams());
                            babyAdapter.notifyDataSetChanged();

                        }
                    });

                    break;
            }
        }
    };
    private SocietyBabyContract.IModel model;
    private SocietyBabyContract.IPresenter presenter;
    private SocietyBabyAdapter babyAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initData() {
        super.initData();

        model = new SocietyBabyModel();
        presenter = new SocietyBabyPresenter(this, model);
        presenter.initData(initParams());

    }

    private void initPullTo() {
        ILoadingLayout startLabels = pullBabySociety
                .getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        ILoadingLayout endLabels = pullBabySociety.getLoadingLayoutProxy(
                false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示
    }

    private HashMap<String, String> initParams() {
        HashMap<String, String> params = new HashMap<>();
        params.put("apiVersion","215");
        params.put("channelId","baidu");
        params.put("devicesId","867581020760386");
        params.put("terminalName","8676-A01");
        params.put("terminalType","2");
        params.put("token","01a30e837881ad7a17b8fce9def84956");
        params.put("version","2.1.6");
        params.put("current",String.valueOf(current));
        params.put("length","20");
        params.put("methodName","guild_getGuildBabyList");
        return params;
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_society_baby;
    }


    @OnClick(R.id.iv_back)
    public void onClick() {
        finish();
    }

    @Override
    public void OnSeccessSocietyBaby(SocietyBabyBean societyBabyBean) {
        Message message = handler.obtainMessage(BABY_BEAN,societyBabyBean.getData().getBabyList());
        handler.sendMessage(message);
    }

    @Override
    public void OnFailSocietyBaby(String str) {

    }
}
